package com.tadpole.music.view.fragment.me;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.permissions.RxPermissions;
import com.tadpole.music.R;
import com.tadpole.music.app.MyApplication;
import com.tadpole.music.bean.WeChatUserInfoBean;
import com.tadpole.music.bean.home.UserInfoBean;
import com.tadpole.music.config.Constant;
import com.tadpole.music.iView.home.UserInfoIView;
import com.tadpole.music.iView.wechat.BindWeChatIView;
import com.tadpole.music.iView.wechat.UserInfoWeChatIView;
import com.tadpole.music.presenter.home.UserInfoPresenter;
import com.tadpole.music.presenter.wechat.BindWeChatPresenter;
import com.tadpole.music.presenter.wechat.UserInfoWeChatPresenter;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.NoDoubleClickListener;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.home.ClockInActivity;
import com.tadpole.music.view.activity.me.MyPointsActivity;
import com.tadpole.music.view.activity.me.yun.YunLoginActivity;
import com.tadpole.music.view.activity.web.H5DaiLiActivity;
import com.tadpole.music.view.activity.login.LoginActivity;
import com.tadpole.music.view.activity.me.MemberCenterActivity;
import com.tadpole.music.view.activity.me.MyStudentActivity;
import com.tadpole.music.view.activity.me.SettingActivity;
import com.tadpole.music.view.activity.me.UserInfoActivity;
import com.tadpole.music.view.activity.web.H5JiaoFuActivity;
import com.tadpole.music.view.fragment.base.BaseFragment;
import com.tadpole.music.view.widget.NiceImageView;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import io.reactivex.functions.Consumer;

/**
 * 我的
 * Created By 孙岩
 * on 2020/9/8
 */
public class MeFragment extends BaseFragment implements UserInfoIView, UserInfoWeChatIView, BindWeChatIView {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;

    //老师
    private LinearLayout llTeacher;
    //我的学生
    private RelativeLayout rlMyStudent;
    //蝌蚪代理
    private RelativeLayout rlDaiLi;
    //教辅材料
    private RelativeLayout rlJiaoFu;
    //绑定微信
    private LinearLayout llBind;
    private TextView tvBind;
    //学生
    //学生 基本信息
    private RelativeLayout rlUserInfo;

    //设置
    private RelativeLayout rlSetting;

    //头像
    private NiceImageView ivImage;
    //昵称
    private TextView tvName;
    //会员中心
    private ImageView ivVip, ivVip1;

    //打卡
    private RelativeLayout rlDaka;

    //云考试
    private RelativeLayout rlYun;

    //我的积分 点击
    private LinearLayout llJiFen;
    //我的积分 数量
    private TextView tvMeJiFen;
    //获取用户信息
    private UserInfoPresenter userInfoPresenter;
    //微信登录获取微信信息的presenter
    private UserInfoWeChatPresenter userInfoWeChatPresenter;
    //绑定微信
    private BindWeChatPresenter bindWeChatPresenter;
    //one
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv4;
    private ImageView iv5;
    //是否绑定微信
    private boolean isBindWeChat = false;
    //接收openId和access_token的广播
    private WxGetOpenidBroadcast mBroadcast;
    //数据源
    private UserInfoBean.DataBean bean;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {//微信
                try {
                    String openId = msg.getData().getString("wxOpenid");
                    String access_token = msg.getData().getString("access_token");

                    SpUtil.getInstance(getActivity()).putString(Constant.KEY_WX_OPENID, openId);
                    SpUtil.getInstance(getActivity()).putString(Constant.ACCESS_TOKEN, access_token);

                    userInfoWeChatPresenter.getUserInfo(openId, access_token);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };

    public static MeFragment newInstance(String param1, String param2) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_me;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initView(View mRootView) {
        ivVip = mRootView.findViewById(R.id.ivVip);
        ivVip1 = mRootView.findViewById(R.id.ivVip1);
        rlSetting = mRootView.findViewById(R.id.rlSetting);
        llTeacher = mRootView.findViewById(R.id.llTeacher);
        rlDaiLi = mRootView.findViewById(R.id.rlDaiLi);
        rlJiaoFu = mRootView.findViewById(R.id.rlJiaoFu);
        rlMyStudent = mRootView.findViewById(R.id.rlMyStudent);
        rlUserInfo = mRootView.findViewById(R.id.rlUserInfo);
        rlDaka = mRootView.findViewById(R.id.rlDaka);
        llJiFen = mRootView.findViewById(R.id.llJiFen);
        tvMeJiFen = mRootView.findViewById(R.id.tvMeJiFen);
        llBind = mRootView.findViewById(R.id.llBind);
        tvBind = mRootView.findViewById(R.id.tvBind);
        ivImage = mRootView.findViewById(R.id.ivImage);
        tvName = mRootView.findViewById(R.id.tvName);
        rlYun = mRootView.findViewById(R.id.rlYun);
        iv1 = mRootView.findViewById(R.id.iv1);
        iv2 = mRootView.findViewById(R.id.iv2);
        iv3 = mRootView.findViewById(R.id.iv3);
        iv4 = mRootView.findViewById(R.id.iv4);
        iv5 = mRootView.findViewById(R.id.iv5);

        initListeners();

        mBroadcast = new WxGetOpenidBroadcast();
        getActivity().registerReceiver(mBroadcast, new IntentFilter(Constant.BIND_WX_GET_OPENID11111));

        userInfoPresenter = new UserInfoPresenter();
        userInfoPresenter.attachView(this);
        userInfoPresenter.getUserInfo();

        //获取微信用户信息
        userInfoWeChatPresenter = new UserInfoWeChatPresenter();
        userInfoWeChatPresenter.attachView(this);

        //绑定微信
        bindWeChatPresenter = new BindWeChatPresenter();
        bindWeChatPresenter.attachView(this);

    }

    //初始化监听器
    private void initListeners() {
        //基本信息
        rlUserInfo.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }
        });

        //会员中心
        ivVip.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), MemberCenterActivity.class);
                        intent.putExtra("flag", "2");
                        startActivity(intent);
                    }

                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //会员中心
        ivVip1.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), MemberCenterActivity.class);
                        intent.putExtra("flag", "2");
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //设置
        rlSetting.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
//                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
//                        Intent intent = new Intent(getActivity(), LoginActivity.class);
//                        startActivity(intent);
//                    } else {
                        Intent intent = new Intent(getActivity(), SettingActivity.class);
                        startActivity(intent);
//                    }
                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //我的学生
        rlMyStudent.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), MyStudentActivity.class);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //昵称
        tvName.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //头像
        ivImage.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                        startActivity(intent);
                    }
                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //云考试
        rlYun.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), YunLoginActivity.class);
                        startActivity(intent);
                    }
                }else{
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //教辅材料
        rlJiaoFu.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        andPermission(2);
                    }
                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //蝌蚪代理
        rlDaiLi.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        andPermission(1);
                    }
                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }

            }
        });

        //绑定微信
        llBind.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (Mutils.isNetworkAvailable()) {
                    if (isBindWeChat) {
                        ToastUtils.show("微信已绑定");
                    } else {
                        if (!MyApplication.mWxApi.isWXAppInstalled()) {
                            ToastUtils.show("您还没有安装微信客户端");
                            return;
                        }
                        SendAuth.Req req = new SendAuth.Req();
                        req.scope = "snsapi_userinfo";//获取用户个人信息
                        req.state = "2";
                        MyApplication.mWxApi.sendReq(req);
                    }
                } else {
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //打卡
        rlDaka.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        andPermission();
                    }
                }else{
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        //我的积分
        llJiFen.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
                    if (SpUtil.getInstance(getActivity()).getString(Constant.TOKEN, "").equals("")) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getActivity(), MyPointsActivity.class);
                        intent.putExtra("point",bean.getPoints());
                        startActivity(intent);
                    }
                }else{
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });


        iv1.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
//                    getState(1);
                }else{
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });


        iv2.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
//                    getState(2);
                }else{
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        iv3.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
//                    getState(3);
                }else{
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        iv4.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
//                    getState(4);
                }else{
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });

        iv5.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(Mutils.isNetworkAvailable()){
//                    getState(5);
                }else{
                    ToastUtils.show("请检查您的网咯设置");
                }
            }
        });
    }

    //判断买没买
    private void getState(int college_id){
        for (int i = 0; i < bean.getCollege().size(); i++) {
            if (bean.getCollege().get(i).getCollege_id() == college_id) {

            }else{
                Intent intent = new Intent(getActivity(), MemberCenterActivity.class);
                intent.putExtra("flag", "2");
                startActivity(intent);
            }
        }
    }

    //权限
    @SuppressLint("CheckResult")
    private void andPermission() {
        RxPermissions rxPermission = new RxPermissions(getActivity());
        rxPermission
                .request(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                ).subscribe(new Consumer<Boolean>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //请求权限成功
                if (aBoolean) {
                    Intent intent = new Intent(getActivity(), ClockInActivity.class);
                    startActivity(intent);
                } else {//失败
                    String msg = "请点击【去授权】打开相应权限！";
                    showAlert(msg);
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        userInfoPresenter.getUserInfo();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showUserInfo(UserInfoBean.DataBean bean) {
        this.bean = bean;
        if (bean.getCollege().size() == 0) {
            iv1.setImageResource(R.mipmap.one_gray_icon);
            iv2.setImageResource(R.mipmap.two_gray_icon);
            iv3.setImageResource(R.mipmap.three_gray_icon);
            iv4.setImageResource(R.mipmap.four_gray_icon);
            iv5.setImageResource(R.mipmap.five_gray_icon);
        } else {
            for (int i = 0; i < bean.getCollege().size(); i++) {
                if (bean.getCollege().get(i).getCollege_id() == 1) {
                    iv1.setImageResource(R.mipmap.one_color_icon);
                }
                if (bean.getCollege().get(i).getCollege_id() == 2) {
                    iv2.setImageResource(R.mipmap.two_color_icon);
                }
                if (bean.getCollege().get(i).getCollege_id() == 3) {
                    iv3.setImageResource(R.mipmap.three_color_icon);
                }
                if (bean.getCollege().get(i).getCollege_id() == 4) {
                    iv4.setImageResource(R.mipmap.four_color_icon);
                }
                if (bean.getCollege().get(i).getCollege_id() == 5) {
                    iv5.setImageResource(R.mipmap.five_color_icon);
                }
            }
        }

        if (bean.isBind_wxapp()) {
            tvBind.setText("已绑定");
            tvBind.setTextColor(getResources().getColor(R.color.colorMain));
//            isBindWeChat = true;
        } else {
            tvBind.setText("未绑定");
            tvBind.setTextColor(getResources().getColor(R.color.color_33));
//            isBindWeChat = false;
        }

        if (bean.getRole().contains("2")) {//表明是老师
            rlUserInfo.setVisibility(View.GONE);
            rlMyStudent.setVisibility(View.VISIBLE);
            rlJiaoFu.setVisibility(View.VISIBLE);
            if (bean.getIs_agent() == 1) {//表明是代理
                rlDaiLi.setVisibility(View.VISIBLE);
            } else {
                rlDaiLi.setVisibility(View.GONE);
            }
        } else {//表明是学生
            rlMyStudent.setVisibility(View.GONE);
            rlUserInfo.setVisibility(View.VISIBLE);
            rlDaiLi.setVisibility(View.GONE);
            rlJiaoFu.setVisibility(View.GONE);
        }

        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.mipmap.app_icon)
                .error(R.mipmap.app_icon)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        if (bean.getAvatar().equals("")) {
            Glide.with(this).load(R.mipmap.app_icon).apply(requestOptions).into(ivImage);
        } else {
            Glide.with(this).load(bean.getAvatar()).apply(requestOptions).into(ivImage);
        }
        if (bean.getNickname().equals("")) {
            tvName.setText("蝌蚪音基会员");
        } else {
            tvName.setText(bean.getNickname());
        }

        tvMeJiFen.setText(bean.getPoints()+"积分");

        SpUtil.getInstance(getActivity()).putString(Constant.USER_AVATAR, bean.getAvatar());
        SpUtil.getInstance(getActivity()).putString(Constant.USER_NICKNAME, bean.getNickname());

    }

    @Override
    public void noLogin() {
        Glide.with(this).load(R.mipmap.app_icon).into(ivImage);
        tvName.setText("请登录");
        iv1.setVisibility(View.GONE);
        iv2.setVisibility(View.GONE);
        iv3.setVisibility(View.GONE);
        iv4.setVisibility(View.GONE);
        iv5.setVisibility(View.GONE);
        llJiFen.setVisibility(View.GONE);
        rlUserInfo.setVisibility(View.GONE);
        rlMyStudent.setVisibility(View.GONE);
        llTeacher.setVisibility(View.GONE);
        llBind.setVisibility(View.GONE);
        rlDaiLi.setVisibility(View.GONE);
        rlYun.setVisibility(View.GONE);
        SpUtil.getInstance(getActivity()).putString(Constant.TOKEN, "");
    }

    //权限
    @SuppressLint("CheckResult")
    private void andPermission(final int flag) {
        RxPermissions rxPermission = new RxPermissions(getActivity());
        rxPermission
                .request(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                ).subscribe(new Consumer<Boolean>() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                //请求权限成功
                if (aBoolean) {
                    if (flag == 1) {//蝌蚪代理
                        Intent intent = new Intent(getActivity(), H5DaiLiActivity.class);
                        intent.putExtra("title", "蝌蚪代理");
                        startActivity(intent);
                    } else if (flag == 2) {//教辅材料
                        Intent intent = new Intent(getActivity(), H5JiaoFuActivity.class);
                        intent.putExtra("title", "教辅材料");
                        startActivity(intent);
                    }
                } else {//失败
                    String msg = "请点击【去授权】打开相应权限！";
                    showAlert(msg);
                }
            }
        });
    }

    //授权
    public void showAlert(String msg) {
        new AlertDialog.Builder(getActivity()).setMessage(msg).
                setPositiveButton("去授权",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
                                intent.setData(uri);
                                startActivity(intent);
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setCancelable(true).show();
    }

    //绑定微信的回调
    @Override
    public void showUserInfo(WeChatUserInfoBean bean) {
        bindWeChatPresenter.bindWeChat(
                bean.getOpenid(),
                bean.getUnionid(),
                bean.getNickname(),
                bean.getCountry(),
                bean.getLanguage(),
                bean.getProvince(),
                bean.getCity(),
                bean.getHeadimgurl(),
                bean.getSex());
    }

    @Override
    public void showBindWeChatResult() {
        ToastUtils.show("绑定微信成功");
        userInfoPresenter.getUserInfo();
    }

    /**
     * 微信登录返回用户信息的广播
     */
    public class WxGetOpenidBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null) {
                return;
            }
            String wxOpenid = intent.getExtras().getString(Constant.KEY_WX_OPENID);
            String access_token = intent.getExtras().getString(Constant.ACCESS_TOKEN);
            Message msg = Message.obtain();
            msg.what = 1;
            Bundle bundle = new Bundle();
            bundle.putString("wxOpenid", wxOpenid);
            bundle.putString("access_token", access_token);
            msg.setData(bundle);
            mHandler.sendMessage(msg);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mBroadcast != null) {
            getActivity().unregisterReceiver(mBroadcast);
            mBroadcast = null;
        }
    }
}
